package com.iscc.maoke.controller;

import com.google.gson.Gson;
import com.iscc.maoke.domain.User;
import com.iscc.maoke.service.UserService;
import com.iscc.maoke.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet({"/register.do", "/login.do", "/checkAccount.do"})
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        UserService userService = new UserServiceImpl();

        PrintWriter out = response.getWriter();

        // /MaoKe/register.do
        String uri = request.getRequestURI();

        Gson gosn = new Gson();

        String action = uri.substring(uri.lastIndexOf("/") + 1);
        Map<String, Object> resultMap = new HashMap<>();
        if ("register.do".equals(action)) {
            //执行注册操作
            String account = request.getParameter("account");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");

            System.out.println(account + "   " + password + "   " + phone);

            User user = new User(account, password, phone);
            int result = userService.addUser(user);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "注册成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "注册失败");
            }

        } else if ("login.do".equals(action)) {
            String account = request.getParameter("account");
            String password = request.getParameter("password");

            User user = userService.login(account);
            if (user == null) {
                resultMap.put("code", 202);
                resultMap.put("msg", "账号不存在");
            } else if (!user.getPassword().equals(password)) {
                resultMap.put("code", 201);
                resultMap.put("msg", "密码输入有误");
            } else {
                resultMap.put("code", 200);
                resultMap.put("msg", "登录成功");
                resultMap.put("result", user);
            }
        } else if ("checkAccount.do".equals(action)) {
            String account = request.getParameter("account");
            User user = userService.login(account);
            if (user != null) {
                resultMap.put("code", 201);
                resultMap.put("msg", "账号已经存在，请更换");
            } else {
                resultMap.put("code", 200);
                resultMap.put("msg", "账号可以使用");
            }
        }
        System.out.println("resultMap = " + resultMap);
        String str = gosn.toJson(resultMap);
        System.out.println("str = " + str);
        out.print(str);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
